;
(function() {
	jadeSystem.validateInstall();

	/**
	 * 游戏控制器
	 * @param {Object} scenes 场景
	 * @param {Object} sprites 精灵
	 */
	JadeEngine.GameController = function(scenes, sprites, keyMapping, closeDefault) {
		if (typeof scenes === "undefined") {
			this.scenes = [];
		}

		if (typeof scenes === "object") {
			this.scenes = [];
			this.scenes.push(scenes)
		}
		
		this.statusSuppend = false;
		this.keyMapping = keyMapping;
		this.isDefaultInit = true;
		this.currentScence = this.scenes[0];
		this.visibleWindow = new JadeEngine.VisibleWindow(this.currentScence, 2, 2);

		this.keyboardProcessor = function(e) {
			if (jadeSystem.isUndefine(this.keyMapping)) {
				this.keyMapping = {};
			}
			var keynum = null;
			if (window.event) {
				keynum = e.keyCode
			} else if (e.which) {
				keynum = e.which
			}

			//ENTER键开始游戏

			if (jadeSystem.isUndefine(this.keyMapping.startCode)) {
				this.keyMapping.startCode = 13;
			}

			if (keynum == this.keyMapping.startCode) {
				this.start();
			}

			if (jadeSystem.isUndefine(this.keyMapping.suspendCode)) {
				this.keyMapping.suspendCode = 32;
			}

			//空格键暂停游戏
			if (keynum == this.keyMapping.suspendCode) {
				if (this.statusSuppend) {
					this.resume();
					this.statusSuppend = false;
				} else {
					this.suspend();
					this.statusSuppend = true;
				}
			}

			if (jadeSystem.isUndefine(this.keyMapping.upCode)) {
				this.keyMapping.upCode = 38;
			}

			if (keynum == this.keyMapping.upCode) {
				this.moveUp();
			}

			if (jadeSystem.isUndefine(this.keyMapping.downCode)) {
				this.keyMapping.downCode = 40;
			}

			if (keynum == this.keyMapping.downCode) {
				this.moveDown();
			}

			if (jadeSystem.isUndefine(this.keyMapping.leftCode)) {
				this.keyMapping.leftCode = 37;
			}

			if (keynum == this.keyMapping.leftCode) {
				this.moveLeft();
			}

			if (jadeSystem.isUndefine(this.keyMapping.rightCode)) {
				this.keyMapping.rightCode = 39;
			}

			if (keynum == this.keyMapping.rightCode) {
				this.moveRight();
			}

			if (jadeSystem.isUndefine(this.keyMapping.exitCode)) {
				this.keyMapping.exitCode = 27;
			}

			if (keynum == this.keyMapping.exitCode) {
				this.stop();
			}
		}

		this.moveRight = function() {
			var params = {};
			params.eventType = "move";
			params.directionCode = "right";
			params.controller = this;
			jadeSystem.notifyArrayAndResult(this.currentScence.sprites, "onEvent", params);
			jadeSystem.notifyAndResult(this.visibleWindow, "onEvent", params);
			this.render();
		}

		this.moveLeft = function() {
			var params = {};
			params.eventType = "move";
			params.directionCode = "left";
			params.controller = this;
			jadeSystem.notifyArrayAndResult(this.currentScence.sprites, "onEvent", params);
			jadeSystem.notifyAndResult(this.visibleWindow, "onEvent", params);
			this.render();
		}

		this.moveDown = function() {
			var params = {};
			params.eventType = "move";
			params.directionCode = "down";
			params.controller = this;
			jadeSystem.notifyArrayAndResult(this.currentScence.sprites, "onEvent", params);
			jadeSystem.notifyAndResult(this.visibleWindow, "onEvent", params);
			this.render();
		}

		this.moveUp = function() {
			var params = {};
			params.eventType = "move";
			params.directionCode = "up";
			params.controller = this;
			jadeSystem.notifyArrayAndResult(this.currentScence.sprites, "onEvent", params);
			jadeSystem.notifyAndResult(this.visibleWindow, "onEvent", params);
			this.render();
		}

		this.start = function() {
			this.statusSuppend = false;
			var params = {};
			params.eventType = "game";
			params.directionCode = "start";
			params.controller = this;
			jadeSystem.notifyArrayAndResult(this.scenes, "onEvent", params);
			jadeSystem.notifyArrayAndResult(this.currentScence.sprites, "onEvent", params);
			jadeSystem.notifyAndResult(this.visibleWindow, "onEvent", params);
			this.clock = JadeEngine.system.clock;
			JadeEngine.system.timer.start();
			this.render();
		}

		this.stop = function() {
			var params = {};
			params.eventType = "game";
			params.directionCode = "stop";
			params.controller = this;
			JadeEngine.system.timer.stop();
			jadeSystem.notifyArrayAndResult(this.scenes, "onEvent", params);
			jadeSystem.notifyArrayAndResult(this.currentScence.sprites, "onEvent", params);
			jadeSystem.notifyAndResult(this.visibleWindow, "onEvent", params);
			JadeEngine.system.clock = this.clock;
		}

		this.resume = function() {
			var params = {};
			params.eventType = "game";
			params.directionCode = "resume";
			params.controller = this;
			jadeSystem.notifyArrayAndResult(this.scenes, "onEvent", params);
			jadeSystem.notifyArrayAndResult(this.currentScence.sprites, "onEvent", params);
			jadeSystem.notifyAndResult(this.visibleWindow, "onEvent", params);
			JadeEngine.system.timer.start();
			this.render();
		}

		this.suspend = function() {
			var params = {};
			params.eventType = "game";
			params.directionCode = "suspend";
			params.controller = this;
			jadeSystem.notifyArrayAndResult(this.scenes, "onEvent", params);
			jadeSystem.notifyArrayAndResult(this.currentScence.sprites, "onEvent", params);
			jadeSystem.notifyAndResult(this.visibleWindow, "onEvent", params);
			JadeEngine.system.timer.suspend();
		}

		this.render = function() {
			this.visibleWindow.renderWindow();
			var params = {};
			params.eventType = "render";
			params.controller = this;
			jadeSystem.notifyArrayAndResult(this.currentScence.sprites, "onEvent", params);
		}

		this.setCurrentSecenes = function(currentScences) {
			if (typeof currentScences === "undefined") {
				this.scenes = [];
			}

			if (typeof currentScences === "object") {
				this.scenes = [];
				this.scenes.push(currentScences)
			}
			this.currentScence = this.scenes[0];
		}

		this.init = function() {
			var that = this;
			window.document.onkeydown = function(e) {
				that.keyboardProcessor(e);
			}
		}
		if (this.isDefaultInit && jadeSystem.isUndefine(closeDefault)) {
			this.init();
		}
	}

}(window));
